package com.notepad.ui;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.notepad.R;
import com.notepad.db.dao.DatabaseDaoImp;
import com.notepad.db.entity.DBNoteEntity;
import com.notepad.domain.NoteEntity;
import com.notepad.domain.Notepad;
import com.notepad.util.Constant;
import com.notepad.util.DataUtil;
import com.notepad.util.FileUtil;
import com.notepad.view.LinedEditText;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

public class WriteNewNoteActivity extends Activity {
    private ActionBar mActionBar;
    private LinedEditText mLinedEditText;
    private EditText mTitleEditText;
    private String mTitle;
    private String mContent;
    private DataUtil mDataUtil;
    private DatabaseDaoImp mDatabaseDaoImp;
    private Notepad notepad;
    private String tag = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.write_new_note);
        getIntentValue();
        initActionBar();
        init();

    }


    private void initActionBar() {
        mActionBar = getActionBar();
        mActionBar.setBackgroundDrawable(new ColorDrawable(getResources()
                .getColor(R.color.pink_normal)));
        // 设置返回箭头不显示
        mActionBar.setDisplayHomeAsUpEnabled(false);
        // 设置左上角系统图标、标题不显示
        mActionBar.setDisplayShowHomeEnabled(false);
        mActionBar.setDisplayShowTitleEnabled(false);
        // 显示自定义视图
        mActionBar.setDisplayShowCustomEnabled(true);
        ActionBar.LayoutParams lp = new ActionBar.LayoutParams(
                ActionBar.LayoutParams.MATCH_PARENT,
                ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);
        View actionBarLayout = LayoutInflater.from(this).inflate(
                R.layout.actionbar_layout, null);

        mActionBar.setCustomView(actionBarLayout, lp);
        actionBarButtonOnClick();
    }

    private void init() {

        mLinedEditText = (LinedEditText) findViewById(R.id.edit_content);
        mLinedEditText.setTextColor(getResources().getColor(R.color.edit_color));
        mLinedEditText.setTextSize(24);

        mTitleEditText = (EditText) findViewById(R.id.edit_title);

        mTitleEditText.setText(mTitle);
        mLinedEditText.setText(mContent);

        mDataUtil = new DataUtil(WriteNewNoteActivity.this);

    }

    public void actionBarButtonOnClick() {
        //actionBar左侧返回按钮点击事件
        mActionBar.getCustomView().findViewById(R.id.actionbar_left_back_imgbtn)
                .setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        String title = mTitleEditText.getText().toString();
                        String content = (String) mLinedEditText.getText().toString();
                        if (TextUtils.isEmpty(title) && TextUtils.isEmpty(content)) {
                            finish();
                        } else {
                            if (!(title.equals(mTitle)) || !(content.equals(mContent))) {
                                showAlertDialog();
                            } else {
                                finish();
                            }
                        }
                    }
                });

        //actionBar右侧保存按钮点击事件
        mActionBar.getCustomView().findViewById(R.id.actionbar_right_save_imgbtn)
                .setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        saveNote();
                    }
                });

    }

    public void showAlertDialog() {
        new AlertDialog.Builder(WriteNewNoteActivity.this).setTitle("提示")
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setMessage("您还有内容尚未保存，是否保存?")
                .setNegativeButton(R.string.cancle, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        finish();
                    }
                })
                .setPositiveButton(R.string.save_content, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        saveNote();
                    }
                }).show();

    }


    private void saveNote() {
        String title = mTitleEditText.getText().toString();
        String content = (String) mLinedEditText.getText().toString();
        String msg = "";
        if (TextUtils.isEmpty(title) && TextUtils.isEmpty(content)) {
            Toast.makeText(this, "您的笔记为空，不能保存", Toast.LENGTH_LONG).show();
        } else {
            Notepad note = new Notepad(System.currentTimeMillis(),title, content,getTime());
            if (tag.equals("home")) {
                mDataUtil.insertNote(note);
                msg = "保存成功";
            } else if (tag.equals("list")) {
                note.setNoteId(notepad.getNoteId());
                mDataUtil.updateNote(note);
                msg = "更新成功";
            }
            Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
            setResult(10, null);
            finish();
        }
    }


    public static String getTime() {
        SimpleDateFormat formatter = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
        String str = formatter.format(new Date());
        return str;
    }

    private void getIntentValue() {
        Intent intent = getIntent();
        if (intent != null) {
            tag = intent.getStringExtra("tag");
            notepad = (Notepad) intent.getSerializableExtra(Constant.Note_Pad_Flag);
            if (notepad != null) {
                mTitle = notepad.getTitle();
                mContent = notepad.getContent();
            } else {
                mTitle = "";
                mContent = "";
            }
        }
    }
}
